package com.toplabs.unashu.common;

import com.toplabs.unashu.annotation.BackoffProcess;
import com.toplabs.unashu.annotation.Recover;
import com.toplabs.unashu.annotation.RetryableProcess;
import com.toplabs.unashu.exception.RetryableException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author toplabs
 * @since 2022/11/22
 * <p>This java file was created by toplabs in 2022/11/22.
 * The following is the description information about this file:</p>
 * <p>description: 接口测试</p>
 */
@Slf4j
@Component
public class InterfaceTest {
    ThreadLocal<Integer> local = ThreadLocal.withInitial(() -> 0);

    @RetryableProcess(backoff = @BackoffProcess(value = 1000, maxDelay = 3000, multiplier = 1.5), recover = "testRecover")
    public void test() {
        local.set(local.get() + 1);
        log.debug("业务正在进行中, 第{}次.", local.get());
        throw new RetryableException(500, String.format("第%s次失败", local.get()));
    }

    @Recover
    public void testRecover(RetryableException e) {
        log.error("3次重试失败, exception message:{}", e.getMessage());
    }
}